﻿using Kairos.Models;
using Kairos.Properties;
using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace Kairos.Controllers
{
    public class GrapheController : Controller
    {
        private myContext db = new myContext();

        //
        // GET: /Graphe/Alarme

        [AccessControl(Roles.All)]
        public ActionResult Alarme()
        {
            var graphes = db.Graphes.Where(d => DateTime.Compare(DateTime.Today, d.DateExpiration) >= 0);

            return View(graphes.ToList());
        }

        //
        // POST: /Graphe/Alarme

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Alarme(int id)
        {
            Graphe graphe = db.Graphes.Find(id);
            if (graphe == null)
            {
                return HttpNotFound();
            }

            graphe.DateExpiration = DateTime.Now.AddDays(Settings.Default.GrapheExpiration);

            db.Entry(graphe).State = EntityState.Modified;
            db.SaveChanges();

            var graphes = db.Graphes.Where(d => DateTime.Compare(DateTime.Today, d.DateExpiration) >= 0);

            return View(graphes.ToList());
        }

        //
        // GET: /Graphe/Details/5

        [AccessControl(Roles.All)]
        public ActionResult Details(int id = 0)
        {
            Graphe graphe = db.Graphes.Find(id);
            if (graphe == null)
            {
                return HttpNotFound();
            }
            return View(graphe);
        }

        //
        // GET: /Graphe/Create

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Graphe/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create(Graphe graphe)
        {
            if (ModelState.IsValid && graphe.Image!=null)
            {
                graphe.DateCreation = DateTime.Now;
                graphe.DateModification = DateTime.Now;

                graphe.DateExpiration = DateTime.Now.AddDays(Settings.Default.GrapheExpiration);

                graphe.UserCreation = User.Identity.Name;
                graphe.UserModification = User.Identity.Name;

                graphe.Revision = 1;

                db.Graphes.Add(graphe);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = graphe.ID });
            }

            return View(graphe);
        }

        //
        // GET: /Graphe/Edit/5

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(int id = 0)
        {
            Graphe graphe = db.Graphes.Find(id);
            if (graphe == null)
            {
                return HttpNotFound();
            }
            return View(graphe);
        }

        //
        // POST: /Graphe/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(Graphe graphe)
        {
            if (ModelState.IsValid)
            {
                graphe.DateModification = DateTime.Now;

                graphe.DateExpiration = DateTime.Now.AddDays(Settings.Default.GrapheExpiration);

                graphe.UserModification = User.Identity.Name;

                graphe.Revision += 1;

                if (graphe.Image != null)
                {
                    graphe.Image.ID = graphe.ImageID;
                    db.Entry(graphe.Image).State = EntityState.Modified;
                }

                db.Entry(graphe).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new { id = graphe.ID });
            }
            return View(graphe);
        }

        //
        // POST: /Graphe/Delete/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Delete(int id)
        {
            Graphe graphe = db.Graphes.Find(id);
            db.Graphes.Remove(graphe);
            db.SaveChanges();
            return RedirectToAction("Graphe", "Search");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}